package com.example.wwk.graduationpractice.Broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * Created by wwk on 2017/4/12.
 */

public class MessageComeBroadReceiver extends BroadcastReceiver {
    /*
    消息到来的广播
    */
    public static final String MSG_COME = "com.example.wwk.graduationpractice.MSG_COME";
    private DealMsgListener dealMsgListener;
    private String from;
    private String body;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(MSG_COME)) {     //判读action是否符合过滤条件
            try {
                from = intent.getStringExtra("from");
                body = intent.getStringExtra("body");
                dealMsgListener.deal(from,body);
                Log.d("123", "onReceive: MessageCome " + from + " " + body);
            } catch (UnsupportedOperationException e) {
                e.printStackTrace();
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
    }
    /*
    处理消息的监听接口实现
    */
    public void setDealMsgListener(DealMsgListener dealMsgListener) {
        this.dealMsgListener = dealMsgListener;
    }

    public interface DealMsgListener {
        void deal(String from, String body);
    }
}
